package com.example.vv.model.searchDetail;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.example.vv.R;
import com.example.vv.fragmentWork.entity.SearchEntity;
import com.example.vv.fragmentWork.mvp.MvpActivity;
import com.example.vv.model.searchDetail.presenter.SIDPresneter;
import com.example.vv.model.setupItemDetails.base.contract.SIDContract;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

public class SIDActivity extends MvpActivity<SIDContract.View, SIDPresneter> implements SIDContract.View {

    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.hid_web)
    WebView hidWeb;

    @Override
    public int initLayout() {
        return R.layout.activity_h_i_d;
    }

    @Override
    public SIDPresneter initPresenter() {
        return new SIDPresneter();
    }

    @Override
    public void initView() {
        titleContent.setMaxEms(12);
        titleContent.setSingleLine(true);
        titleContent.setEllipsize(TextUtils.TruncateAt.END);
        EventBus.getDefault().register(this);
        intiWeb();
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void gette(SearchEntity.DatasBean entity){
        titleContent.setText(entity.getTitle());
        hidWeb.loadUrl(entity.getLink());
    }

    private void intiWeb() {
        hidWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url == null){
                    return false;
                }
                try{
                    if(!url.startsWith("http://") && !url.startsWith("https://")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                        return true;
                    }
                }catch (Exception e){//防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
                    return true;//没有安装该app时，返回true，表示拦截自定义链接，但不跳转，避免弹出上面的错误页面
                }

                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
//                view.loadUrl(url);
//                return false;
            }
        });
        WebSettings settings = hidWeb.getSettings();
        settings.setSupportMultipleWindows(true);
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setAllowFileAccess(true);
        settings.setSupportZoom(false);
        settings.setDisplayZoomControls(false);
        settings.setBuiltInZoomControls(false);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    }

    @Override
    public void initData() {

    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleView() {
        return null;
    }


    @Override
    public Context getMContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}